package com.snail.poker.web.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.snail.poker.model.Equipment;
import com.snail.poker.model.Member;
import com.snail.poker.uitls.Constant;
import com.snail.poker.uitls.DateUtil;

/**
 * 登录验证的拦截器实现
 * 
 * @author Administrator
 * 
 */
public class AuthInterceptor extends AbstractInterceptor {
	private static Log log = LogFactory.getLog(AuthInterceptor.class);
	private String excludeMethods;

	public String[] getExcludeMethods() {
		if (this.excludeMethods.contains(",")) {
			return this.excludeMethods.split(",");
		}
		return new String[] { excludeMethods };
	}

	public void setExcludeMethods(String excludeMethods) {
		this.excludeMethods = excludeMethods;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest) invocation
				.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}

		log.warn("访问时间：[ " + DateUtil.getDate() + " ]");
		log.warn("请求地址：[ " + ip + " ]");
		log.warn("请求端口：[ " + request.getRemotePort() + " ]");

		String actionName = invocation.getProxy().getActionName();// 获取当前访问的action名字
		log.warn("当前访问的Action接口名称：" + actionName);
		if (null != this.getExcludeMethods()
				&& this.getExcludeMethods().length > 0) {
			for (String exclude : this.getExcludeMethods()) {
				if (exclude.equals(actionName)) {
					return invocation.invoke();
				}
			}
		}
		Map<String, Object> session = invocation.getInvocationContext()
				.getSession();
		// 判断用户是否以移动设备来请求服务
		int platform = Integer
				.valueOf(request.getParameter(Constant.PLATFORM) == null ? "0"
						: request.getParameter(Constant.PLATFORM));
		if (platform == Constant.PLATFORM_DEVICES_KEY) {
			Equipment equipment = (Equipment) session
					.get(Constant.EQUIPMENT_SESSION_KEY);
			if (equipment == null) {
				return Action.NONE;
			}
			log.warn("神秘用户使用的移动设备MAC地址：[ " + equipment.getMacAddress()
					+ " ]登录平台");
		} else {
			Member me = (Member) session.get(Constant.MEMBER_SESSION_KEY);
			if (null == me) {
				return Action.LOGIN;
			}
			log.warn("神秘用户：[ " + me.getMemberLoginName() + " ]以浏览器方式登录平台");
		}
		return invocation.invoke();
	}
}